package Controller.UserController;

import Utils.BaseMessage;
import Utils.isString;
import ViewOption.userVo;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", urlPatterns = {
        "/api/User/isNoLogin/RegisterServlet"
})
public class RegisterServlet extends HttpServlet {
    private final isString isString = new isString();
    private final ViewOption.userVo userVo = new userVo();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tel = request.getParameter("tel");
        String password = request.getParameter("password");
        String cpassword = request.getParameter("cpassword");
        String nickname = request.getParameter("nickname");
        String email = request.getParameter("email");
        String username = request.getParameter("username");
        if (isString.IsEmpty(tel, password, cpassword, nickname, email, username)) {
            response.getWriter().print(JSON.toJSONString(new BaseMessage(2, "参数为空", null)));
            return;
        }
        if (!password.equals(cpassword)) {
            response.getWriter().print(JSON.toJSONString(new BaseMessage(1, "请确认密码是否输入正确", null)));
            return;
        }
        response.getWriter().print(JSON.toJSONString(userVo.insertUserByRegister(tel, password, nickname, email, username)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
